#include "CombatManager.hpp"
#include "Utils.hpp"
#include "MonsterFactory.hpp"

using namespace MonsterFactory;


CombatManager* CombatManager::instance = NULL;
CombatManager::Garbo CombatManager::garbo;

void CombatManager::Fight(Player* player, IMonster* monster) {
  
  bool result = true;

  // tasks
  while(true) {
    result = monster->PerformTask();

    if (result) break;
    else if (player->GetLives() > 0) {
      // int ret = system("clear");
      std::cout << "Retry...\n";
      player->UpdateLives(-1);
    } else {
      break;
    }
  }
  
  if (result) {
    player->LevelUp();
    if (player->GetLevel() % 5 == 0) player->UpdateLives(1);
  } else 
    player->SetAsDead(true);
}

bool CombatManager::BTPGarden(Player* player) {
  // level 1->20, 20 levels
  unsigned int level = 1;
  IMonsterFactory* factory = PlantMonsterFactory::GetInstance();
  IMonster* monster = NULL;
  
  for (; level <=20; ++level) {
    if (level <= 10)
      monster = factory->CreatePhaseOneMonster(level);
    else
      monster = factory->CreatePhaseTwoMonster(level);      

    Fight(player, monster);

    delete monster;

    if (player->IsDead()) break;
  }

  if (player->IsDead()) {
    return false;
  } else
    return true;
}


bool CombatManager::NeteaseFarm(Player* player) {
  // level 1->20, 20 levels
  unsigned int level = 1;
  IMonsterFactory* factory = AnimalMonsterFactory::GetInstance();
  IMonster* monster = NULL;
  
  for (; level <=20; ++level) {
    if (level <= 10)
      monster = factory->CreatePhaseOneMonster(level);
    else
      monster = factory->CreatePhaseTwoMonster(level);      

    Fight(player, monster);

    delete monster;

    if (player->IsDead()) break;
  }

  if (player->IsDead()) {
    return false;
  } else
    return true;
}
bool CombatManager::InstanceZone(Player* player) {
  // level 1->20, 20 levels
  unsigned int level = 1;
  IMonsterFactory* factory = NULL;
  IMonster* monster = NULL;
  
  for (; level <=20; ++level) {
    int randomFactory = RandomGenerator(0,2);
    switch(randomFactory) {
    case 0:
      factory = AnimalMonsterFactory::GetInstance();
      break;
    case 1:
      factory = PlantMonsterFactory::GetInstance();
      break;
    case 2:
      factory = EliteMonsterFactory::GetInstance();
      break;
    default:
      factory = AnimalMonsterFactory::GetInstance();
      break;
    }
    if (level <= 10)
      monster = factory->CreatePhaseOneMonster(level);
    else
      monster = factory->CreatePhaseTwoMonster(level);      

    Fight(player, monster);

    delete monster;

    if (player->IsDead()) break;
  }

  if (player->IsDead()) {
    return false;
  } else
    return true;
}
